package com.yonghui;

import lombok.extern.slf4j.Slf4j;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.concurrent.TimeUnit;

/**
 * 软引用OOM测试
 */
@Slf4j
public class SoftReferenceOOM {

    public static void main(String[] args) {
        log.info("========创建软引用=========");
//        Student stu = new Student("张三", 22);
//        SoftReference sf = new SoftReference(stu);
//        stu=null;
        ReferenceQueue<Student> queue=new ReferenceQueue<>();
        SoftReference sf=new SoftReference(new Student("张三",22),queue);
        log.info("打印对象{}",sf.get());
        log.info("========执行gc=======");
        System.gc();
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("========After gc========");
        log.info("打印对象{}",sf.get());

        try {
            byte[] b=new byte[1024*1024*10];

        } catch (Throwable e) {
            e.printStackTrace();
            log.info("打印对象{}",sf.get());
        }
    }

}
